using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Dogfight2008.Common
{
  public class ColoredTriangleSoup : IEnumerable<ColoredTriangle>
  {
    List<ColoredTriangle> list = new List<ColoredTriangle>();

    public void Add(ColoredTriangle coloredTriangle)
    {
      list.Add(coloredTriangle);
    }

    public int TriangleCount
    {
      get
      {
        return list.Count;
      }
    }

    public ColoredTriangle this[int index]
    {
      get
      {
        return list[index];
      }
      set
      {
        list[index] = value;
      }
    }

    #region IEnumerable<ColoredTriangle> Members

    public IEnumerator<ColoredTriangle> GetEnumerator()
    {
      return list.GetEnumerator();
    }

    IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      return list.GetEnumerator();
    }

    #endregion

    public delegate ColoredTriangle ColoredTriangleMapDelegate(ColoredTriangle coloredTriangle);

    public void Map(ColoredTriangleMapDelegate function)
    {
      for (int i = 0; i < TriangleCount; i++)
        this[i] = function(this[i]);
    }

    public override bool Equals(object obj)
    {
      ColoredTriangleSoup rhs = obj as ColoredTriangleSoup;
      if (rhs == null)
        return false;

      if (TriangleCount != rhs.TriangleCount)
        return false;

      for (int i = 0; i < list.Count; i++)
        if (!this[i].Equals(rhs[i]))
          return false;

      return true;
    }

    public override int GetHashCode()
    {
      unchecked { return list.GetHashCode(); }
    }
  }
}
